<?php

/**
 * idea actions.
 *
 * @package    project_midnight
 * @subpackage idea
 * @author     AlvaroLara
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ideaActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('idea', 'randomIdea');
  }

  public function executeRandomIdea(sfWebRequest $request)
  {
    if($request->hasParameter('id'))
    {
      $this->idea = IdeaPeer::retrieveByPk($request->getParameter('id'));
    }
    else
    {
      $this->idea = IdeaPeer::getRandomIdea();
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new IdeaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new IdeaForm();

    $this->processForm($request,$this->form);

    $this->setTemplate('new');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'Your idea has been created' : 'Your idea has been modified.';

      $Idea = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $Idea)));

      $this->getUser()->setFlash('notice', $notice);

      $this->redirect('@idea_show?id='.$Idea->getId());
    }
    else
    {
      $this->getUser()->setFlash('error', 'Sorry but some errors occured. Check and try again', false);
    }
  }

}
